package com.chinaoly.baotou.common.utils;


import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 时间区间工具类
 */
public class TimeUtil {

    public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static SimpleDateFormat formatdd = new SimpleDateFormat("yyyy-MM-dd");
    public static Calendar c = Calendar.getInstance();

    /**
     * 过去一个月
     *
     * @return
     */
    public static String lastMonth() {

        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        String mon = format.format(m);
        return mon;
    }


    /**
     * 过去一年
     *
     * @return
     */
    public static String lastYear() {
        c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        String year = format.format(y);
        return year;
    }

    /**
     * 过去一周
     *
     * @return
     */
    public static String lastWeek() {
        c.setTime(new Date());
        c.add(Calendar.WEEK_OF_MONTH, -1);
        Date y = c.getTime();
        c.setTime(y);
        c.add(Calendar.DATE, 1);
        y =  c.getTime();
        String week = format.format(y);
        return week;
    }

    /**
     * 根据周获取最后一天日期
     *
     * @return
     */
    public static String getDateByWeek(String week) {
        String date = "";
        try {
            DateTimeFormatter formatterWeek = DateTimeFormatter.ofPattern("YYYY-w e");
            DateTimeFormatter formatterDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate endDate = LocalDate.parse(week + " 7", formatterWeek);
            date = formatterDate.format(endDate);
        } catch (Exception e) {
            System.out.println(e);
            date = week;
        }

        return date;
    }

    /**
     * 传入时间格式 获取当前时间
     *
     * @param formatPattern
     */
    public static String getCurrentTime(String formatPattern) {
        Date d = new Date();
        if (StringUtils.isEmpty(formatPattern)) {
            return format.format(d);
        }
        SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
        return sdf.format(d);
    }

    public static void main(String[] args) throws Exception {
        System.out.println(getCurrentMonth());
        System.out.println(lastMonth());
    }


    /**
     * 获取当天开始时间
     *
     * @return
     */
    public static String getStartTime() {
        Calendar todayStart = Calendar.getInstance();

        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return format.format(todayStart.getTime());
    }

    /**
     * 获取当天结束时间
     *
     * @return
     */
    public static String getEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return format.format(todayEnd.getTime());
    }

    /**
     * 获取入参时间24小时前的时间
     *
     * @param dateEnd
     * @return
     */
    public static String getOneDayBefore(Date dateEnd) {
        Calendar date = Calendar.getInstance();
        date.setTime(dateEnd);
        date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
        return format.format(date.getTime());
    }

    /**
     * 获取入参时间24小时前的时间
     *
     * @param dateEnd
     * @return
     */
    public static Date getOneDayBeforeDate(Date dateEnd) {
        Calendar date = Calendar.getInstance();
        date.setTime(dateEnd);
        date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
        return date.getTime();
    }

    /**
     * 专用方法 按制定时间切割一段时间
     *
     * @param dateType
     * @param dBegin
     * @param dEnd
     * @param time
     * @return
     * @throws Exception
     */
    public static List<String> findDates(String dateType, Date dBegin, Date dEnd, int time) {
        List<String> listDate = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("dd-HH");
        listDate.add(format.format(dBegin));
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(dEnd);
        while (calEnd.after(calBegin)) {
            if ("H".equals(dateType)) {
                calBegin.add(Calendar.HOUR, time);
            }
            if ("M".equals(dateType)) {
                calBegin.add(Calendar.MONTH, time);
            }
            if ("D".equals(dateType)) {
                calBegin.add(Calendar.DATE, time);
            }
            if ("N".equals(dateType)) {
                calBegin.add(Calendar.MINUTE, time);
            }
            if (calEnd.after(calBegin)) {
                listDate.add(format.format(calBegin.getTime()));
            } else {
                // listDate.add(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(calEnd.getTime()));
            }

        }
        return listDate;
    }

    public static String getMonthStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return format.format(calendar.getTime());
    }

    /**
     * 获取当年的开始时间戳
     *
     * @return
     */
    public static String getYearStartTime() {
        Long timeStamp = System.currentTimeMillis();
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.setTimeInMillis(timeStamp);
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return format.format(calendar.getTimeInMillis());
    }
    public static String getMonthEnd(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.SECOND,59);
        return format.format(calendar.getTime());
    }
    /**
     * Todo 计算某个时间，加或者减时间
     * @param fromTime yyyy-MM-dd HH:mm:ss
     * @param overTime MINUTE
     * @param format 时间格式
     * @return overQTimeKey  yyyyMMddHHmmssSSSSSS
     */
    public static String CalTimeDifference(String fromTime, int overTime,String format) {
        if (StringUtils.isEmpty(format)){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = dateFormat.parse(fromTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, overTime);//这里是分钟，如果需要，可以更换为day,moth
        Date date1 = calendar.getTime();
        return dateFormat.format(date1);
    }

    /**
     * 根据类型获取开始时间
     * @return
     */
    public static String getTimeByType(String timeType) {
        if (StringUtils.isEmpty(timeType)){
            return lastYear();
        }
        if (timeType.equals("day")){
            return getStartTime();
        }
        if (timeType.equals("week")){
            return lastWeek();
        }
        if (timeType.equals("month")){
            return lastMonth();
        }
        if (timeType.equals("year")){
            return lastYear();
        }
        return lastYear();
    }

    /**
     * 时间格式化星期几
     * @param datetime
     * @return
     * @throws ParseException
     */
    public static String  dateToWeek(String datetime) throws ParseException {
        String rq = datetime;
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(rq);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1 < 0 ? 0 : cal.get(Calendar.DAY_OF_WEEK) - 1;
        String week= weekDays[w];
        return week;
    }

    /**
     * 根据指定时间获取日历
     * @param year
     * @param month
     * @return
     */
    public static List<String> getMonthFullDay(int year , int month){
        SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
        List<String> fullDayList = new ArrayList<>(32);
        // 获得当前日期对象
        Calendar cal = Calendar.getInstance();
        cal.clear();// 清除信息
        cal.set(Calendar.YEAR, year);
        // 1月从0开始
        cal.set(Calendar.MONTH, month-1 );
        // 当月1号
        cal.set(Calendar.DAY_OF_MONTH,1);
        int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int j = 1; j <= count ; j++) {
            fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime()));
            cal.add(Calendar.DAY_OF_MONTH,1);
        }
        return fullDayList;
    }

    /**
     * 获取当前月第一天
     * @return
     */
    public static String getCurrentMonth(){
        // 获取当前年份、月份、日期
        Calendar cale = null;
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        String firstday = formatdd.format(cale.getTime());
        return firstday;
    }


}
